package tugas.akhir.jason;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

class AlmagHelper extends SQLiteOpenHelper{

	private static final String DATABASE_NAME="middasdatabase3.db";
	private static final int SCHEMA_VERSION=1;
	
	public AlmagHelper(Context context) {
		super(context, DATABASE_NAME, null, SCHEMA_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, idglobal TEXT, alamatglobal TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// no-op, since will not be called until 2nd schema
		// version exists
	}

	public Cursor getAll() {
		return(getReadableDatabase()
						.rawQuery("SELECT _id, idglobal, alamatglobal FROM almag ORDER BY idglobal",null));
	}

	public Cursor getById(String id) {
		String[] args={id};
		return(getReadableDatabase()
						.rawQuery("SELECT _id, idglobal, alamatglobal FROM almag WHERE _ID=?",args));
	}
	
	public void insert(String idglobal, String alamatglobal) {
		ContentValues cv=new ContentValues();
		cv.put("idglobal", idglobal);
		cv.put("alamatglobal", alamatglobal);
		getWritableDatabase().insert("almag", "idglobal", cv);
	}
	
	public void update(String id, String idglobal, String alamatglobal) {
		ContentValues cv=new ContentValues();
		String[] args={id};
		
		cv.put("idglobal", idglobal);
		cv.put("alamatglobal", alamatglobal);

		getWritableDatabase().update("almag", cv, "_ID=?", args);
	}
	
	public String getIdglobal(Cursor c) {
		return(c.getString(1));
	}
	
	public String getAlamatglobal(Cursor c) {
		return(c.getString(2));
	}
	
	
	
	
}
